package traveldream.web.beans;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;

import traveldream.ejb.packetmanagement.EscursioniMgr;
import traveldream.ejb.packetmanagement.HotelMgr;
import traveldream.ejb.packetmanagement.PacketMgr;
import traveldream.ejb.packetmanagement.VoloMgr;
import traveldream.ejb.packetmanagement.dto.EscursioneDTO;
import traveldream.ejb.packetmanagement.dto.HotelDTO;
import traveldream.ejb.packetmanagement.dto.PacketDTO;
import traveldream.ejb.packetmanagement.dto.VoloDTO;

@ManagedBean(name="searchPktBean")
@SessionScoped
public class SearchPktBean {   
 
	@EJB
	private PacketMgr packetMgr;
	
	private PacketDTO packet = new PacketDTO();
	private PacketDTO selectedPacket = new PacketDTO();
	private List<PacketDTO> packetList = new ArrayList<PacketDTO>();
	
	@EJB
	private VoloMgr voloMgr;
	
	@EJB
	private HotelMgr hotelMgr;
	
	@EJB
	private EscursioniMgr escursioniMgr;
	
	private VoloDTO voloAndata = new VoloDTO();
	private VoloDTO voloRitorno= new VoloDTO();
	private HotelDTO hotel = new HotelDTO();
	private List<EscursioneDTO> escursioni = new ArrayList<EscursioneDTO>();
	private EscursioneDTO selectedEsc = new EscursioneDTO();
	
	public String searchByPid(){
		packetList = packetMgr.findByPid(packet.getPid());
		return "risultatiRicerca.xhtml";
	}  
	
	public String searchByDestinazione(){
		packetList = packetMgr.findByDestinazione(packet.getDestinazione());
		return "risultatiRicerca.xhtml";
	}
	
	public String searchByDate(){
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			packetList = packetMgr.findByDate(dateFormat.parse(packet.getDataInizio()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "/risultatiRicerca.xhtml";
	}
	
	public String searchCombo(){
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			packetList = packetMgr.findCombo(packet.getDestinazione(), dateFormat.parse(packet.getDataInizio()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "risultatiRicerca.xhtml";
	}
	
	public String searchByPidUtente(){
		packetList = packetMgr.findByPid(packet.getPid());
		return "risultatiRicercaUtente.xhtml";
	}  
	
	public String searchByDestinazioneUtente(){
		packetList = packetMgr.findByDestinazione(packet.getDestinazione());
		return "risultatiRicercaUtente.xhtml";
	}
	
	public String searchByDateUtente(){
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			packetList = packetMgr.findByDate(dateFormat.parse(packet.getDataInizio()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "risultatiRicercaUtente.xhtml";
	}
	
	public String searchComboUtente(){
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			packetList = packetMgr.findCombo(packet.getDestinazione(), dateFormat.parse(packet.getDataInizio()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "risultatiRicercaUtente.xhtml";
	}
	
	public SearchPktBean(){
	}
	

	public PacketDTO getPacket() {
		return packet;
	}

	public void setPacket(PacketDTO packet) {
		this.packet = packet;
	}

	public List<PacketDTO> getPacketList() {
		return packetList;
	}

	public void setPacketList(List<PacketDTO> packetList) {
		this.packetList = packetList;
	}
	
	public void getDettagliVoli(){
		voloAndata = voloMgr.findVoloByVid(selectedPacket.getVidAndata());
		voloRitorno = voloMgr.findVoloByVid(selectedPacket.getVidRitorno());
	}
	
	public void getDettagliHotel(){
		hotel = hotelMgr.findHotelByHid(selectedPacket.getHidHotel());
	}
	
	public void getDettagliEscursioni(){
		escursioni = escursioniMgr.findExcsByPid(selectedPacket.getPid());
	}
	
	public VoloDTO getVoloAndata() {
		return voloAndata;
	}

	public void setVoloAndata(VoloDTO voloAndata) {
		this.voloAndata = voloAndata;
	}

	public VoloDTO getVoloRitorno() {
		return voloRitorno;
	}

	public void setVoloRitorno(VoloDTO voloRitorno) {
		this.voloRitorno = voloRitorno;
	}

	public HotelDTO getHotel() {
		return hotel;
	}

	public void setHotel(HotelDTO hotel) {
		this.hotel = hotel;
	}

	public List<EscursioneDTO> getEscursioni() {
		return escursioni;
	}

	public void setEscursioni(List<EscursioneDTO> escursioni) {
		this.escursioni = escursioni;
	}

	public EscursioneDTO getSelectedEsc() {
		return selectedEsc;
	}

	public void setSelectedEsc(EscursioneDTO selectedEsc) {
		this.selectedEsc = selectedEsc;
	}

	public PacketDTO getSelectedPacket() {
		return selectedPacket;
	}

	public void setSelectedPacket(PacketDTO selectedPacket) {
		this.selectedPacket = selectedPacket;
	}
	
}
